package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Dept;
import cn.wolfcode.domain.Result;
import cn.wolfcode.service.DeptService;
import cn.wolfcode.web.exception.DataException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import java.util.List;
@Slf4j
@Lazy //延迟加载 (第一次使用bean的时候才会交给容器对象)
@Scope("prototype")
@RestController
@RequestMapping("/depts")

public class DeptController {

    @Autowired
    private DeptService deptService;
    @GetMapping
    public Result list(){
        //查询数据
        /**
         * 调用service
         */
        log.info("部门列表查询");
        List<Dept> list = deptService.list();
        //返回Result
        return Result.success(list);
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) throws Exception {
        //操作
        log.info("删除部门id为 :{}",id);
        deptService.delete(id);
        //响应
        return Result.success();
    }
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        log.info("部门新增: {},{}",dept.getId(),dept.getName());
        //操作
        deptService.add(dept);

        //响应
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        log.info("根据部门id={}查询",id);
        Dept dept = deptService.selById(id);
        return Result.success(dept);
    }
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("部门修改 :{}",dept);
        deptService.update(dept);
        return Result.success();
    }
}
